package com.bzkj.basicinfo.service.basic.impl;

import com.bzkj.basicinfo.domain.basic.BasicCustomer;
import com.bzkj.basicinfo.mapper.BasicCustomerMapper;
import com.bzkj.basicinfo.service.basic.BasicCustomerService;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.model.LoginUser;
import com.bzkj.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author：swj
 * @Desc
 * @Date：2025/9/28 15:30
 */
@Service
public class BasicCustomerServiceImpl implements BasicCustomerService {

    @Autowired
    private BasicCustomerMapper mapper;

    @Override
    public BasicCustomer getById(String id) {
        return mapper.selectById(id);
    }

    @Override
    public List<BasicCustomer> listByCondition(BasicCustomer customer) {
        List<BasicCustomer> customers = mapper.selectByCondition(customer);
        customers.forEach(i->{
            List<String> list = Arrays.asList(i.getCustomerType().split(","));
            i.setTypeList(list);
        });
        return customers;
    }

    @Override
    public AjaxResult add(BasicCustomer customer) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        customer.setCreateBy(loginUser.getUsername());
        customer.setCreateTime(new Date());
        customer.setEnableState("1");
        int i = mapper.insertSelective(customer);
        if (i > 0) {
            return AjaxResult.success("新增成功");
        }
        return AjaxResult.error("新增失败");
    }

    @Override
    public AjaxResult update(BasicCustomer customer) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        customer.setUpdateBy(loginUser.getUsername());
        customer.setUpdateTime(new Date());
        int i = mapper.updateSelective(customer);
        if (i > 0) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    @Override
    public AjaxResult delete(String id) {
        int i = mapper.deleteById(id);
        if (i > 0) {
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

    @Override
    public AjaxResult updateState(String id, String state) {
        int i = mapper.updateEnableSate(id,state);
        if (i > 0) {
            return AjaxResult.success("修改成功");
        }else{
            return AjaxResult.error("修改失败");
        }
    }
}
